package org.yf.samples.web;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.yf.samples.model.CommonMessageResultVO;
import org.yf.samples.model.ExamSubjectVO;
import org.yf.samples.model.Subject;
import org.yf.samples.service.ISubjectService;

@Controller
public class SubjectController {
	
	@Autowired
	private ISubjectService subjectService;
	
	@Autowired
	private LocaleResolver localeResolver;
	
	@ModelAttribute
	public Subject getSubject(){
		return new Subject();
	}
	
	@RequestMapping(value = "/subjectChangeLocale")
	public String changeLocale(HttpServletRequest request, HttpServletResponse response, String localeType){
		if("en_US".equals(localeType)){
			localeResolver.setLocale(request, response, Locale.US);
		}else if("zh_CN".equals(localeType)){
			localeResolver.setLocale(request, response, Locale.CHINA);
		}
		return "redirect:/subjects";
	}

	@RequestMapping("/subjects")
	public String Subjects(Model model){
		List<ExamSubjectVO> list = subjectService.findAllSubjects();
		model.addAttribute("subjectList", list);
		return "subject/subjectList";
	}
	
	@ResponseBody
	@RequestMapping(value = "/subjectAdd", method = RequestMethod.POST)
	public CommonMessageResultVO add(RedirectAttributes redirectAttributes, @Valid ExamSubjectVO subject, BindingResult result){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String, String>();
		resultVO.setSuccess(false);
		if(result.hasErrors()){
			List<ObjectError> errorList = result.getAllErrors();
			for(ObjectError error : errorList){
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			resultVO.setMessageMap(messageMap);
			return resultVO;
		}
		try{
			subjectService.createSubject(subject);
			resultVO.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/subjectEdit", method = RequestMethod.POST)
	public CommonMessageResultVO edit(RedirectAttributes redirectAttributes, @Valid ExamSubjectVO subject, BindingResult result){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String, String>();
		resultVO.setSuccess(false);
		if(result.hasErrors()){
			List<ObjectError> errorList = result.getAllErrors();
			for(ObjectError error : errorList){
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			resultVO.setMessageMap(messageMap);
			return resultVO;
		}
		try{
			subjectService.editSubject(subject);
			resultVO.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getSubjectById", method = RequestMethod.POST)
	public ExamSubjectVO getSubjectById(Integer subId){
		ExamSubjectVO subject = subjectService.getSubjectById(subId);
		return subject;
	}
	
	@ResponseBody
	@RequestMapping(value = "/deleteSubject", method = RequestMethod.POST)
	public CommonMessageResultVO delete(Integer subId){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String, String>();
		resultVO.setSuccess(false);
		try{
			subjectService.deleteSubject(subId);
			resultVO.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
}
